package com.zis.system.server;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zis.starter.core.Constant;
import com.zis.starter.core.ResponseEntity;
import com.zis.starter.core.SearchParams;
import com.zis.system.dao.UserRoleDao;
import com.zis.system.inter.IUserRoleInter;
import com.zis.system.model.UserRole;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Create by wangshen 2018/4/18
 */
@Service
@Transactional("txManager")
public class UserRoleInterImpl implements IUserRoleInter {

    @Autowired
    private UserRoleDao userRoleDao;

    @Override
    public ResponseEntity insert(UserRole model) throws Exception {
        return null;
    }

    @Override
    public ResponseEntity update(UserRole model) throws Exception {
        return null;
    }

    @Override
    public ResponseEntity delete(String id) throws Exception {
        return null;
    }

    @Override
    public ResponseEntity selectById(String id) throws Exception {
        return null;
    }

    @Override
    public PageQuery<UserRole> findByPage(SearchParams params) throws Exception {
        return null;
    }

    @Override
    public ResponseEntity findByCondition(Map params) throws Exception {
        return null;
    }

    /**
     * 用户角色保存
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public ResponseEntity userRoleSave(JSONObject params) throws Exception {
        if (!params.containsKey("userId") || params.getString("userId").equals("")) {
            return ResponseEntity.failure("用户信息为空");
        }
        if (!params.containsKey("userRole") || params.getString("userRole").equals("")) {
            return ResponseEntity.failure("没有需要设置的角色信息");
        }
        String userId = params.getString("userId");
        JSONArray userRoleArray = JSON.parseArray(params.getString("userRole"));
        if (userRoleArray.size() == 0) {
            return ResponseEntity.failure("没有需要设置的角色息");
        }
        List<UserRole> userRoleList = new ArrayList<>();
        for (Object obj : userRoleArray) {
            String roleId = String.valueOf(obj);
            UserRole userRole = new UserRole();
            userRole.setRoleId(roleId);
            userRole.setUserId(userId);
            userRoleList.add(userRole);
        }
        //删除原有记录
        userRoleDao.deleteByUserId(userId);
        //批量写入行的
        userRoleDao.insertBatch(userRoleList);
        return ResponseEntity.success(Constant.DATA_RESULT_SUCCEED);
    }

    /**
     * 根据userId查询角色
     * @param userId
     * @return
     * @throws Exception
     */
    @Override
    public ResponseEntity findByUserId(String userId) throws Exception {
        List<UserRole> userRoleList = userRoleDao.findByUserId(userId);
        if(userRoleList != null && userRoleList.size() > 0) {
            JSONArray userRoleArray = new JSONArray();
            for (UserRole userRole : userRoleList) {
                JSONObject jsonObj = new JSONObject();
                jsonObj.put("roleId", userRole.getRoleId());
                jsonObj.put("userId", userRole.getUserId());
                jsonObj.put("ruId", userRole.getRuId());
                userRoleArray.add(jsonObj);
            }
            return ResponseEntity.success(userRoleArray);
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_NULL);
    }
}
